<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml"
      xmlns:th="http://www.thymeleaf.org"
      lang="zh-cn">
<head>
    <meta content="云之讯,云通讯,云通讯平台" name="keywords"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="user-scalable=no, width=device-width, initial-scale=1, maximum-scale=1"/>
    <title>短信平台运营系统 </title>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0">
    <link th:href="@{/resources/css/bootstrap.min14ed.css?v=3.3.6}" rel="stylesheet">
    <link th:href="@{/resources/css/font-awesome.min93e3.css?v=4.4.0}" rel="stylesheet">
    <!-- jqgrid-->
    <link th:href="@{/resources/css/animate.min.css}" rel="stylesheet">
    <link th:href="@{/resources/css/plugins/sweetalert/sweetalert.css}" rel="stylesheet">
    <link th:href="@{/resources/js/plugins/jqgrid/css/ui.jqgrid-bootstrap.css}" rel="stylesheet">
    <link th:href="@{/resources/css/style.min862f.css?v=4.1.0}" rel="stylesheet">
    <style>
        /* Additional style to fix warning dialog position */
        #alertmod_table_list_2 {
            top: 900px !important;
        }
    </style>
</head>
<body class="gray-bg"><div th:replace="stat :: statCode"></div>
<div class="wrapper wrapper-content animated fadeInRight">
        <div class="col-sm-12">
            <div class="ibox-content">
                <div class="ibox-content">代理商 &nbsp;  <span id="agent_id_name"  style="font-weight: 900;color: #003399;"></span>  &nbsp;  折扣设置</div>
                <form role="form" class="form-inline">
                    <div class="form-group   col-md-offset-7">
                        <input type="text" placeholder="产品名称" id="product_name" name="product_name" class="form-control">
                        <input id="OEM_GJ_SMS_DISCOUNT" type="hidden" th:value="${OEM_GJ_SMS_DISCOUNT}">
                        <input id="agentId" type="hidden" th:value="${agentId}">
                        <input id="agentName" type="hidden" th:value="${agentName}">
                    </div>
                    <div class="form-group">
                        <label for="product_type">产品类型:</label>
                        <select id="product_type" class="form-control" name="product_type">
                            <option value="">全部</option>
                            <option value="3">验证码</option>
                            <option value="4">通知</option>
                            <option value="1">营销</option>
                            <option value="2">国际</option>
                            <option value="0">行业</option>
                        </select>
                    </div>
                    <div class="form-group">
                        <input style='display:none' />
                        <span>&nbsp;&nbsp;<button type="button" class="btn btn-sm btn-danger" onclick="search()">搜索</button></span>
                    </div>
                </form>

            </div>
            <div class="ibox-content">
                <div class="jqGrid_wrapper">
                    <table id="table_list"></table>
                    <div id="pager_list"></div>
                </div>
            </div>
            <form role="form" class="form-inline">
                <div class="form-group">
                    <span>&nbsp;&nbsp;<button type="button" class="btn btn-sm btn-danger" onclick="commit()">提交</button></span>
                    <span>&nbsp;&nbsp;<button type="button" class="btn btn-sm btn-error" onclick="goback()">返回</button></span>
                </div>
            </form>
        </div>
    </div>
</div>
<script th:src="@{/resources/js/hex_ha_ha_ha_ha_ha.js}"></script>
<script th:src="@{/resources/js/jquery.min.js?v=2.1.4}"></script>
<script th:src="@{/resources/js/bootstrap.min.js?v=3.3.6}"></script>
<script th:src="@{/resources/js/plugins/peity/jquery.peity.min.js}"></script>
<script th:src="@{/resources/js/content.min.js?v=1.0.0}"></script>
<script th:src="@{/resources/js/date_format.min.js?v=1.0.0}"></script>
<script th:src="@{/resources/js/jquery.form.js?v=1.0.0}"></script>
<script th:src="@{/resources/js/plugins/jqgrid/js/i18n/grid.locale-cn.js}"></script>
<script th:src="@{/resources/js/plugins/jqgrid/js/jquery.jqGrid.min.js}"></script>
<script th:src="@{/resources/js/plugins/sweetalert/sweetalert.min.js}"></script>
<script th:src="@{/resources/js/plugins/validate/jquery.validate.min.js}"></script>
<script th:src="@{/resources/js/layer/layer.js}"></script>
<script th:src="@{/resources/js/smsp.validate.js}"></script>
<script th:src="@{/resources/js/common.js?v=1.0.0}"></script>
<script th:src="@{/resources/js/auth.js?v=1.0.0}"></script>

<script th:inline="javascript">
    /*<![CDATA[*/

    var data4Discount;
    var productInfoPoList=[];
    var msgTime = 1500;
    var tag = 'tag-1';
    var gjdiscount = $("#OEM_GJ_SMS_DISCOUNT").val();
    var agentId = $("#agentId").val();
    var agentName = $("#agentName").val();

    function search(){
        var productName = $("#product_name").val();
        var productType = $("#product_type").val();
        var agentId = $("#agentId").val();

        $.ajax({
            type: "POST",
            dataType: "json",
            async: false,
            url: /*[[@{/accountInfo/agent/discount/list}]]*/,
            data:{
                agentId:agentId,
                productName:productName,
                productType:productType
            },
            success: function(result){
            }
        });
        productInfoPoList=[];


        $("#table_list").jqGrid('setGridParam',{
            datatype:'json',
            postData: {
                agentId:agentId,
                productName:productName,
                productType:productType
            }, //发送数据
        }).trigger("reloadGrid"); //重新载入
    }

    function checkOne(checker){
        var status = $(checker).prop("checked");
        if(!status){
            var product_id = $(checker).data("pid");
            for(var i=0;i<productInfoPoList.length;i++){
                var existProduct = productInfoPoList[i];
                if(existProduct.productId==product_id){
                    productInfoPoList.splice(i,1);
                    break;
                }
            }
        }
    }
    function goback(){
        history.go(-1);
    }

    function commit(){
        for(var i=0;i<productInfoPoList.length;i++){
            var existProduct = productInfoPoList[i];
            if(existProduct.product_type!=2){
                if(isNaN(existProduct.discountPrice)){
                    layer.msg("折扣率只能是数字，请检查后再提交", {icon: 2,time: msgTime});
                    return ;
                }
                if(!_Auth.isFourdecimal(existProduct.discountPrice)){
                    layer.msg("折后价/折扣率只能是0-1之间的小数，最多保留四位", {icon: 2,time: msgTime});
                    return;
                }
            }else if(existProduct.product_type ==2){
                if(isNaN(existProduct.gjSmsDiscount)){
                    layer.msg("折扣率只能是数字，请检查后再提交", {icon: 2,time: msgTime});
                    return ;
                }
                if(!_Auth.isFourdecimal(existProduct.gjSmsDiscount)){
                    layer.msg("折后价/折扣率只能是0-1之间的小数，最多保留四位", {icon: 2,time: msgTime});
                    return;
                }
            }

        }


        var content = {};
        content.productInfoPoList = productInfoPoList;

        if(content.productInfoPoList==undefined||content.productInfoPoList.length==0){
            return;
        }
        layer.confirm("是否确定设置折扣？", function(index){

            confirmSubmitData(content);

            layer.close(index);
        });
    }


    function confirmSubmitData(content){
        for(var i=0;i<content.productInfoPoList.length;i++){
            var existProduct = productInfoPoList[i];
            if(existProduct.product_type!=2){
                if(existProduct.discountPrice==''||isNaN(existProduct.discountPrice)){
                    product_name = existProduct.product_name;
                    var msgCheck ="折扣率小于0，请重新填写！";
                    layer.alert(msgCheck, function(index){
                        layer.close(index);
                    });
                    return;
                }
            }else if(existProduct.product_type==2){
                if(existProduct.gjSmsDiscount==''||isNaN(existProduct.gjSmsDiscount)){
                    product_name = existProduct.product_name;
                    var msgCheck ="折扣率小于0，请重新填写！";
                    layer.alert(msgCheck, function(index){
                        layer.close(index);
                    });
                    return;
                }
            }

        }



        var jsonText = JSON.stringify(productInfoPoList);


        $.ajax({
            type: "POST",
            url:/*[[@{/accountInfo/agent/discount/save}]]*/,
            data: jsonText,
            contentType: "application/json",
            success: function(data){

                if(data.success){
                    layer.msg(data.msg, {icon: 1,time: msgTime});
                    search();
                }else{
                    var product_id=data.data;
                    var product_type_name;
                    var product_name;
                    var msg = data.msg;
                    for(var i=0;i<productInfoPoList.length;i++){
                        var existProduct = productInfoPoList[i];
                        if(existProduct.product_id==product_id){
                            product_type_name = existProduct.product_type_name;
                            product_name = existProduct.product_name;
                            msg = msg;
                            break;
                        }
                    }
                    layer.alert(msg, function(index){
                        layer.close(index);
                    });
                }
            }

        });

    }

    $(document).ready(function () {
        $.jgrid.defaults.styleUI = "Bootstrap";
        $("#table_list").jqGrid({
            url:/*[[@{/accountInfo/agent/discount/list}]]*/,
            mtype: 'POST',
            datatype: "json",
            postData: {
                agentId:agentId
            },
            jsonReader : {
                root:"data",
                page: "currentPage",
                total: "totalPage",
                records: "totalRecord"
            },
            height: 'auto',
            autowidth: true,
            shrinkToFit: true,
            autoScroll: true,
            rowNum: 10,
//            rowList: [10, 20, 30,50],
            colNames: ["","产品ID(隐藏)","产品名称", "产品类型", "运营商", "区域","到期时间","短信单价（元）","折扣率（隐藏）", "折后价"],
//            colNames: ["","产品ID(隐藏)","产品名称", "产品类型", "运营商", "区域","到期时间","短信单价（元）"/*,"折扣率（隐藏）"*/, "折后价"],
            colModel: [
                {name: "&nbsp;&nbsp;",align: "left",sortable:false,width:5,
                    formatter: function (cellvalue, options, rowObject) {
                        var product_id = rowObject.productId;
                        return "<input type='checkbox'   id='"+product_id+"'  onclick='checkOne(this)' data-pid='"+product_id+"'/>";
                    }
                },
                {name: "productId",align: "left",sortable:false,width:20,hidden:true},
                {name: "productName",align: "left",sortable:false,width:20},
                {name: "productType", align: "left",sortable:false,width:20,
                    formatter: function (cellvalue, options, rowObject) {
                        var productType = rowObject.productType;
                        if(productType==0)
                            return '行业';
                        if(productType==1)
                            return '营销';
                        if(productType==2)
                            return '国际';
                        if(productType==3)
                            return '验证码';
                        if(productType==4)
                            return '通知';
                        if(productType==7)
                            return 'USSD';
                        if(productType==8)
                            return '闪信';
                        if(productType==9)
                            return '挂机短信';
                    }
                },
                {name: "operatorCode", align: "left",sortable:false,width:10,
                    formatter: function (cellvalue, options, rowObject) {
                        var operatorCode = rowObject.operatorCode;
                        if(operatorCode==0)
                            return '全网';
                        if(operatorCode==1)
                            return '移动';
                        if(operatorCode==2)
                            return '联通';
                        if(operatorCode==3)
                            return '电信';
                        if(operatorCode==4)
                            return '国际';
                    }
                },
                {name: "areaCode", align: "left",sortable:false,width:10,
                    formatter: function (cellvalue, options, rowObject) {
                        var areaCode = rowObject.areaCode;
                        if(areaCode==0)
                            return '全国';
                        if(areaCode==1)
                            return '国际';

                    }
                },
                {name: "dueTime", align: "left",sortable:false,width:15,
                    formatter: function (cellvalue, options, rowObject) {
                        var dueTime = rowObject.dueTime;
                        return dueTime.split(' ')[0]

                    }
                },
                {name: "unitPrice", align: "left",sortable:false,width:15},
                {name: "oemAgentProduct.discountPrice", align: "left",sortable:false,hidden:true},
                {name: "total",index: "total",align: "left",width:30,sortable:false,
                    formatter:function(cellvalue, options, rowObject){
                        var switcher;
                        var price_discount=1;
                        var gjSmsDiscount=1.0000;

                        var product_id = rowObject.productId;
                        var product_type = rowObject.productType;
                        var product_type_name = rowObject.product_type_name;
                        var product_name = rowObject.productName;
                        $("#agent_id_name").text(agentId+'-'+agentName);
                        product_name = $('<div/>').text(product_name).html();
                        if(rowObject.oemAgentProduct){
                            price_discount = rowObject.oemAgentProduct.discountPrice;
                            gjSmsDiscount = rowObject.oemAgentProduct.gjSmsDiscount;
                            var id = rowObject.oemAgentProduct.id;
                        }else {
                            gjSmsDiscount = 1.0000;
                            price_discount = rowObject.unitPrice;
                        }
                        if(!gjSmsDiscount){
                            gjSmsDiscount = 1.0000;
                        }

                        if(product_type != 2){
                            switcher = '<input class="discount" style="width: 55px;text-align:center;" type="text" value="'+price_discount.toFixed(4)+'" data-ptype="'+product_type+'" data-key="discountPrice" data-id="'+id+'" data-pdis="'+price_discount+'" data-pid="'+product_id+'"  data-pname="'+product_name+'"  data-ptname="'+product_type_name+'"/>';
                            switcher += '<span><font size="2" color="">&nbsp;&nbsp;[0~1]的四位小数</font></span>';
                            switcher += '<div data-pid="'+product_id+'" class="low" style="display: none;color: #FF9600; font-size: 11px;">亲，您设置的价格有点低哦</div>';
                            switcher += '<div data-pid="'+product_id+'" class="high" style="display: none;color: #FF9600;font-size: 11px;"">亲，您设置的价格有点高哦</div>';
                            switcher += '<div data-pid="'+product_id+'" class="more" style="display: none;color: red;font-size: 11px;"">请输入0-1之间的值</div>';
                        } else {
                            switcher = '<span>折扣率&nbsp;</span>';
                            switcher += '<input class="discount"  style="width: 55px;text-align:center;" type="text" value="'+gjSmsDiscount.toFixed(4)+'" data-ptype="'+product_type+'" data-key="gjSmsDiscount" data-id="'+id+'" data-pgjdis="'+gjSmsDiscount+'"  data-pid="'+product_id+'"  data-pname="'+product_name+'"  data-ptname="'+product_type_name+'"/>';
                            switcher += '<span class="gjSms" >最终折扣率：<input style="width: 50px;" value="'+(gjdiscount*gjSmsDiscount).toFixed(4)+'" class="gjSmsDiscount" disabled></span>';
                        }
//                        debugger;

                        return switcher;
                    }
                }
            ],
            pager: "#pager_list",
            viewrecords: true,
            hidegrid: false,
            gridComplete:function(){
                $("td").attr("title",'');
                $(".discount").keyup(function(){
                    var product_id = $(this).data("pid");
                    var product_type = $(this).data("ptype");
                    var gjSmsDiscount = $(this).data("pgjdis");

                    var val = $(this).val();
                    if(val<=0.01){
                        $(this).siblings(".low").show();
                        $(this).siblings(".high").hide();
                        $(this).siblings(".more").hide();
                    }else if(val>=0.05&&val<=1){
                        $(this).siblings(".high").show();
                        $(this).siblings(".low").hide();
                        $(this).siblings(".more").hide();
                    }else if(val>1){
                        $(this).siblings(".high").hide();
                        $(this).siblings(".low").hide();
                        $(this).siblings(".more").show();
                    }else {
                        $(this).siblings(".high").hide();
                        $(this).siblings(".low").hide();
                        $(this).siblings(".more").hide();
                    }
                   $(this).siblings(".gjSms").children(".gjSmsDiscount").val((gjdiscount * val).toFixed(4));
                    var key = $(this).data("key");
                    var agentId = $("#agentId").val();
                    $('#'+product_id).prop("checked", true);
                    var value = $(this).val();
                    var product = {};
                    var id = $(this).data("id");
                    product.productId = product_id;
                    product[key] = value;
                    product.agentId = agentId;
                    product.product_type = product_type;

                    if(id && id !== "undefined"){
                        product.id = id;
                    }
                    var existP = false;
                    for(var i=0;i<productInfoPoList.length;i++){
                        var existProduct = productInfoPoList[i];
                        if(existProduct.productId==product_id){
                            existProduct[key] = value;
                            existProduct['product_type'] = product_type;
                            existP = true;
                            break;
                        }
                    }
                    if(!existP){
                        productInfoPoList.push(product);
                    }
                });
            }
        });


        $(document).keypress(function(e) {
            // 回车键事件
            if(e.which == 13) {
                search();
            }
        });
    });
    /*]]>*/

</script>
</body>
</html>